﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BMSQ.Utility
{
    public class DBParameter
    {
        public const string SCENERYORDERSTART = "11_";
        public const string PICKORDERSTART = "12_";
        public const string RESTAURANTORDERSTART = "13_";
        
        public static string GetGuid()
        {
            return Guid.NewGuid().ToString();
        }

        public static string GetSceneryBookID()
        {
            return SCENERYORDERSTART + GetGuid();
        }

        public static string GetPickBookID()
        {
            return PICKORDERSTART + GetGuid();
        }

        public static string GetRestaurantBookID()
        {
            return RESTAURANTORDERSTART + GetGuid();
        }

        public static bool IsSceneryOrder(string bookID)
        {
            return bookID.StartsWith(SCENERYORDERSTART);
        }

        public static bool IsPickOrder(string bookID)
        {
            return bookID.StartsWith(PICKORDERSTART);
        }

        public static bool IsRestaurantOrder(string bookID)
        {
            return bookID.StartsWith(RESTAURANTORDERSTART);
        }

        public static int CreateVerifyCode()
        {
            return new Random().Next(100000, 999999);
        }

        #region 生成订单号
        public static string GetOrderNumber()
        {
            string Number = DateTime.Now.ToString("yyMMddHHmmss");//yyyyMMddHHmmssms
            return Number + Next(1000, 1).ToString();
        }

        private static int Next(int numSeeds, int length)
        {
            // Create a byte array to hold the random value.  
            byte[] buffer = new byte[length];
            // Create a new instance of the RNGCryptoServiceProvider.  
            System.Security.Cryptography.RNGCryptoServiceProvider Gen = new System.Security.Cryptography.RNGCryptoServiceProvider();
            // Fill the array with a random value.  
            Gen.GetBytes(buffer);
            // Convert the byte to an uint value to make the modulus operation easier.  
            uint randomResult = 0x0;//这里用uint作为生成的随机数  
            for (int i = 0; i < length; i++)
            {
                randomResult |= ((uint)buffer[i] << ((length - 1 - i) * 8));
            }
            // Return the random number mod the number  
            // of sides. The possible values are zero-based  
            return (int)(randomResult % numSeeds);
        }
        #endregion
    }
}
